import 'dart:ffi';

import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:whale/helper/utils/color.dart';
import 'package:whale/helper/utils/img_util.dart';
import 'package:whale/pages/home/main/views/market_report.dart';
import 'package:whale/pages/home/main/views/quick_review.dart';
import '../../../helper/route/routes.dart';
import '../../../helper/utils/MyUnderlineTabIndicator.dart';
import '../../app_tab/controller.dart';
import 'logic.dart';
import 'views/explain.dart';
import 'views/hot_people.dart';
import 'views/search.dart';
import 'views/title.dart';

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final logic = Get.put(HomeLogic());
    final state = Get.find<HomeLogic>().state;
    final tabState = Get.find<AppTabController>().state;

    Widget _buildViews() {
      return ListView(
        controller: state.scrollController,
        children: [
          Column(
            children: [
              Stack(
                children: [
                  Image.asset(
                    'agl.png'.imgPathHome,
                    width: 1.sw,
                    fit: BoxFit.cover,
                    height: 190.r,
                  ),
                  Column(
                    children: [
                      const HomeTitleWidget(),
                      HomeSearchItemWidget(),
                    ],
                  ),
                ],
              ),
              HotPeopleWidget(),
              Container(
                key: tabState.intro.keys[2],
                height: 125.r,
                margin: EdgeInsets.symmetric(horizontal: 15.r, vertical: 10.r),
                child: Swiper(
                  autoplay: false,
                  autoplayDelay: 5000,
                  itemCount: state.bannerStr.length,
                  itemBuilder: (context, index) {
                    return ClipRRect(
                      borderRadius: BorderRadius.circular(0.r),
                      child: Image.network(
                        state.bannerStr[index],
                        fit: BoxFit.cover,
                      ),
                    );
                  },
                ),
              ),
              SizedBox(
                height: 10.r,
              ),
              Image.asset(
                'home_bg2.png'.imgPathHome,
                width: 360.r,
              ),
              SizedBox(
                height: 10.r,
              ),
              Stack(
                children: [
                  Container(
                    width: 0.9.sw,
                    margin: EdgeInsets.only(left: 0.1.sw, right: 15.r),
                    padding: EdgeInsets.only(right: 15.r),
                    decoration: BoxDecoration(
                        color: Color(0xFFE1EAFB),
                        borderRadius: BorderRadius.circular(2.r)),
                    height: 110.r,
                    child: Align(
                      alignment: Alignment.centerRight,
                      child: Image.asset(
                        'bg_tag2.png'.imgPathHome,
                        width: 69.r,
                        height: 53.r,
                      ),
                    ),
                  ),
                  Container(
                    width: 0.65.sw,
                    padding: EdgeInsets.all(10.r),
                    margin: EdgeInsets.only(left: 12.r, top: 10.r),
                    decoration: BoxDecoration(
                        gradient: const LinearGradient(
                          colors: [Color(0xFF61A2FF), Color(0xFF2B6CF1)],
                          begin: Alignment.centerLeft,
                          end: Alignment.centerRight,
                        ),
                        borderRadius: BorderRadius.circular(4.r),
                        boxShadow: const [
                          BoxShadow(
                              color: Colors.grey,
                              offset: Offset(0, 1), //阴影y轴偏移量
                              blurRadius: 5, //阴影模糊程度
                              spreadRadius: 1 //阴影扩散程度
                              ),
                        ]),
                    height: 90.r,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            SizedBox(
                              width: 20.r,
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 5.r),
                              child: Text(
                                '新增文件',
                                style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 13.r,
                                    fontWeight: FontWeight.w600),
                              ),
                            ),
                            SizedBox(
                              width: 10.r,
                            ),
                            Text(
                              state.docSum?.newDocumentSum?.newDocumentSum
                                      .toString() ??
                                  '',
                              style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 24.r,
                                  fontWeight: FontWeight.w600),
                            ),
                          ],
                        ),
                        SizedBox(
                          height: 8.r,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            SizedBox(
                              width: 20.r,
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  '系统对接',
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 10.r),
                                ),
                                SizedBox(
                                  height: 5.r,
                                ),
                                Text(
                                  state.docSum?.newDocumentSum?.sysDocumentSum
                                          .toString() ??
                                      '',
                                  style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 11.r,
                                      fontWeight: FontWeight.w600),
                                ),
                              ],
                            ),
                            Container(
                              width: 1.r,
                              color: Color(0xFF8BB8F5),
                              height: 16.r,
                              margin: EdgeInsets.symmetric(horizontal: 15.r),
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  '云上采集',
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 10.r),
                                ),
                                SizedBox(
                                  height: 5.r,
                                ),
                                Text(
                                  state.docSum?.newDocumentSum?.cloudDocumentSum
                                          .toString() ??
                                      '',
                                  style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 11.r,
                                      fontWeight: FontWeight.w600),
                                ),
                              ],
                            ),
                            Container(
                              width: 1.r,
                              color: Color(0xFF8BB8F5),
                              height: 16.r,
                              margin: EdgeInsets.symmetric(horizontal: 15.r),
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  '自主上传',
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 10.r),
                                ),
                                SizedBox(
                                  height: 5.r,
                                ),
                                Text(
                                  state.docSum?.newDocumentSum
                                          ?.ownUploadDocumentSum
                                          .toString() ??
                                      '',
                                  style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 11.r,
                                      fontWeight: FontWeight.w600),
                                ),
                              ],
                            ),
                          ],
                        )
                      ],
                    ),
                  ),
                ],
              ),
              QuickReview(),
              MarketQuickReview(),
              Explainview()
            ],
          )
        ],
      );
    }

    return Scaffold(
      backgroundColor: Color(0xffF7F8FC),
      body: GetBuilder<HomeLogic>(
        builder: (controller) {
          return Stack(
            children: [
              NotificationListener(
                child: SmartRefresher(
                  controller: state.rc,
                  onRefresh: () {
                    logic.initData();
                  },
                  enablePullDown: true,
                  enablePullUp: false,
                  child: _buildViews(),
                ),
                onNotification: (Notification notification) {
                  if (notification is ScrollUpdateNotification) {
                    if ((notification.context?.size?.height ?? 0) <
                        (Get.height - Get.bottomBarHeight - 80.r)) {
                      return true;
                    }
                    if (notification.metrics.pixels > state.offsetY1 - 100.r &&
                        !state.isShowTab) {
                      logic.onScroll(true);
                      return true;
                    } else if (notification.metrics.pixels <
                            state.offsetY1 - 200.r.r &&
                        state.isShowTab) {
                      logic.onScroll(false);
                      return true;
                    }
                  }
                  return true;
                },
              ),
              Visibility(
                visible: state.isShowTab,
                child: Container(
                  // height: 50.r,
                  color: Colors.white,
                  child: SafeArea(
                    child: TabBar(
                      controller: state.home_tabCtrl,
                      isScrollable: false,
                      indicatorColor: ColorsUtil.theme,
                      unselectedLabelColor: ColorsUtil.subTitle,
                      labelColor: ColorsUtil.theme,
                      labelStyle: TextStyle(
                          fontSize: 14.r, fontWeight: FontWeight.w600),
                      unselectedLabelStyle: TextStyle(fontSize: 14.r),
                      indicator: MyUnderlineTabIndicator(
                          borderSide: const BorderSide(
                              width: 3, color: ColorsUtil.theme)),
                      tabs:
                          state.homeTabTitles.map((e) => Tab(text: e)).toList(),
                      onTap: (index) => logic.scrollTo(index),
                    ),
                  ),
                ),
              )
            ],
          );
        },
      ),
    );
  }
}

class TPSliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
  final double max;
  final double min;
  final Color? floatColor;
  final Widget Function(double) builder;
  TPSliverPersistentHeaderDelegate({
    required this.builder,
    required this.max,
    required this.min,
    this.floatColor,
  });

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return Container(
      color: shrinkOffset > 0 ? floatColor : Colors.white,
      child: builder(shrinkOffset),
    );
  }

  @override
  double get maxExtent => max;

  @override
  double get minExtent => min;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}
